use strum_macros::{AsRefStr, Display, EnumProperty, EnumString};
use thiserror::Error;
use strum::EnumProperty;

#[derive(Debug, Error, EnumString, Display,EnumProperty,AsRefStr)]
pub enum ErrorCode {
    #[strum(props(code = "0", desc = "Success"))]
    Success,
    #[strum(props(code = "3001", desc = "invalid argument"))]
    CodeRpcInvalidArgument,
    #[strum(props(code = "3002", desc = "service unavailable"))]
    CodeRpcServiceUnavailable,
    #[strum(props(code = "3003", desc = "service timeout"))]
    CodeRpcServiceTimeout,
    #[strum(props(code = "3004", desc = "unknown method"))]
    CodeRpcUnknownMethod,
    #[strum(props(code = "3005", desc = "require authentication"))]
    CodeRpcRequireAuth,
    #[strum(props(code = "3006", desc = "internal error"))]
    CodeRpcInternalError,
//
// CODE_INVALID_MARKET                        3101
// CODE_USER_ID_INVALID                       3102
// CODE_ORDER_ID_INVALID                      3103
// CODE_STOP_PRICE_ERROR                      3104
// CODE_POSITION_ID_INVALID                   3105
// CODE_ASSET_INVALID                         3106
// CODE_BALANCE_UPDATE_REPEAD                 3107
// CODE_AMOUNT_EXCEED                         3108
#[strum(props(code = "3109", desc = "balance not enough"))]
CodeBalanceNotEnough,

// CODE_TRADER_NOT_ENOUGH                     3110
// CODE_EXCEED_MAX_LIMIT                      3111
// CODE_USER_NOT_MATCH                        3112
// CODE_INVALID_LEVERAGE                      3113
// CODE_ORDER_PRICE_INVALID                   3114
// CODE_POSITION_LIQING                       3115
// CODE_ORDER_KILL                            3116
// CODE_POSITION_WILL_LIQ                     3117
// CODE_PRICE_LIQ_LOWER                       3118
// CODE_PRICE_LIQ_HIGHER                      3119
// CODE_NO_POSITION                           3120
// CODE_CROSS_POSITION                        3121
// CODE_ORDER_EXIST                           3122
// CODE_MARGIN_LESS                           3123
// CODE_MARGIN_SUB_TOOMUCH                    3124
// CODE_STOP_PRICE_ERROR_INDEX                3125
// CODE_STOP_PRICE_ERROR_SIGN                 3126
// CODE_AMOUNT_TOO_SMALL                      3127
// CODE_INVALID_PRICE_SIZE                    3128
// CODE_ORDER_NOT_ONLY_MAKER                  3129
// CODE_MARKET_TRADE_UNAVAILABLE              3130

}

impl ErrorCode {
    pub fn code(&self) -> i32 {
        self.get_str("code")
            .and_then(|s| s.parse().ok())
            .unwrap_or(500)
    }

    pub fn message(&self) -> &'static str {
        self.get_str("message")
            .unwrap_or("Unknown error")
    }
}

